package com.lucky.controller;

import java.util.concurrent.atomic.AtomicLong;

import com.lucky.annotation.Authority;
import com.lucky.annotation.SystemLog;
import com.lucky.pojo.vo.Greeting;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description
 * @Author Jalyson
 * @Version V1.0.0
 * @Since 1.0
 * @Date 2019/6/26 18:38
 */
@RestController
public class GreetingController {

    /**
     * 问候模板
     */
    private static final String template = "Hello, %s!";
    /**
     * 计数器
     */
    private final AtomicLong counter = new AtomicLong();

    /**
     * 无需登录即可访问
     * @param name
     * @return
     */
    @Authority(permission = false)
    @RequestMapping("/greeting")
    @SystemLog(description = "#name+',访问greeting,(无需登录即可访问)'")
    public Greeting greeting(@RequestParam(value = "name", defaultValue = "未认证用户") String name) {
        return new Greeting(counter.incrementAndGet(), String.format(template, name));
    }

    /**
     * 登录并且要有admin 和 manager权限
     * @param name
     * @return
     */

    @Authority(role = { "admin", "manager" })
    @RequestMapping("/greeting2")
    @SystemLog(description = "#name+',访问greeting.（要求admin，manager权限）'")
    public Greeting greeting2(@RequestParam(value = "name", defaultValue = "认证用户") String name) {
        return new Greeting(counter.incrementAndGet(), String.format(template, name));
    }

    /**
     * 登录，并且有manager权限
     * @param name
     * @return
     */
    @Authority(role = { "manager" })
    @RequestMapping("/greeting3")
    @SystemLog(description = "#name+',访问greeting，(访问异常报错，要求manager权限)'")
    public Greeting greetingError(@RequestParam(value = "name", defaultValue = "unknown") String name) {
        int i = 0;
        int a = 10 / i;
        return new Greeting(counter.incrementAndGet(), String.format(template, name));
    }

}